<?php

namespace App\Backend\Http\Controllers;

use App\Tixian;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;

class TixianController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index(Request $request)
    {
        $where['status'] = 0;
        if($request->status){
            $where['status'] = $request->status;
        }
        $data = $request->all();
        if($request->name || $request ->mobile){
              unset($where['status']);
              $where['user_id'] = User::where($data)->value('id');
        }
        if (!empty($request->date)) {
            $date = explode(' - ', $request->date);
            $data['tixians'] = Tixian::where($where)->whereBetween('created_at',[$date[0],$date[1]])->orderBy('id', 'desc')->paginate(20);
        } else {
            $data['tixians'] = Tixian::where($where)->orderBy('created_at', 'desc')->paginate(20);
        }
        return view('backend::tixian.list',$data);
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create($id)
    {
        $data['tixian'] = Tixian::find($id);
        return view('backend::tixian.shenhe',$data);
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(Request $request)
    {
        $data = $request->validate([
            'id' => 'required',
            'status' => 'required',
            'note' => 'required'
        ]);
        $tixian = Tixian::find($data['id']);
        $tixian->update($data);
        if($data['status'] == 1){
            $userinfo = User::find($tixian->user_id);
            $userinfo->notify(new \App\Notifications\Tixian(['message'=>'您在'.$tixian->created_at->toDateString().'申请的'.$tixian->tixianmoney.'元提现已到账']));
            return redirect('backend/redirectnote')->with(['message'=>'审核成功！','status'=>true]);
        }
        return redirect('/backend/tixian');
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('backend::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit()
    {
        return view('backend::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(Request $request)
    {
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy()
    {
    }
}
